/**
 * Created by ax on 2019/3/5.
 */
// 格式化级联菜单数据
export function formatDept (deptList, id, key) {
    for (let i = 0; i < deptList.length; i++) {
        deptList[i].value = deptList[i][id];
        deptList[i].label = deptList[i][key];
        if (Array.isArray(deptList[i].children) && JSON.stringify(deptList[i].children) === '[]') {
            delete deptList[i].children;
        } else {
            formatDept(deptList[i].children, id, key);
        }
    }
    return deptList;
}

// 格式化行政部门数据
export function formatDistrict (data, id, key) {
    data.forEach(item => {
        item.value = item[id];
        item.label = item[key];
        if (Array.isArray(item.children) && item.children.length > 0) {
            formatDistrict(item.children, id, key);
        } else {
            delete item.children;
        }
    });
    return data;
}

// 格式化菜单权限
export function formatAuthors (data, id, key, btns = []) {
    data.forEach(item => {
        item.id = item[id];
        item.label = item[key];
        if (item.menuType === 2) {
            if (Array.isArray(item.children) && item.children.length > 0) {
                btns.push({
                    id: item.id,
                    value: item.children
                });
            }
            delete item.children;
        }
        if (Array.isArray(item.children) && item.children.length > 0) {
            formatAuthors(item.children, id, key, btns);
        }
    });
    return {data, btns};
}

export function getQueryStringByName (name) {
    var result = document.location.search.match(new RegExp('[\?\&]' + name + '=([^\&]+)', 'i'));
    if (result == null || result.length < 1) {
        return '';
    }
    return result[1];
};
